package cl.utalca.robot.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cl.utalca.robot.domain.TestCaseDTO;
import cl.utalca.robot.service.TestCasesService;

@Service
public class TestCasesImpl implements TestCasesService {

	@Autowired
	ServletContext servletContext;
	
	@Override
	public List<TestCaseDTO> createTestCaseList(String testName) {
		
		List<TestCaseDTO> resp = null;

		File inFile = new File(servletContext.getRealPath(File.separator)+ "/resources/in_out/" + testName + ".in");
		File outFile = new File(servletContext.getRealPath(File.separator)+ "/resources/in_out/" + testName + ".sol");
		
		Scanner scannerIn = null;
		Scanner scannerOut = null;
		
		try 
		{
			scannerIn = new Scanner(inFile);
			scannerOut = new Scanner(outFile);
		} 		
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
			return null;
		}
		
		
		resp = new ArrayList<TestCaseDTO>();

		while (scannerIn.hasNextLine())
		{
			String entrada = scannerIn.nextLine();
			if(entrada.trim().equals("")){
				
				continue;
			}
			else{
			
				String salida = scannerOut.nextLine();
				
				TestCaseDTO testCase = new TestCaseDTO(entrada, salida);
				resp.add(testCase);
			}
		}		
		scannerIn.close();
		scannerOut.close();
		
		return resp;
	}

}
